/*
 * Copyright (c) 2011 Denis Tulskiy
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with this work.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.tulskiy.keymaster.osx;

//import javax.swing.KeyStroke;
//import java.awt.event.InputEvent;
import com.tulskiy.keymaster.common.InputEvent;
import java.util.HashMap;
import java.util.Map;

import static com.tulskiy.keymaster.osx.Carbon.*;
//import static java.awt.event.KeyEvent.*;
//import javafx.scene.input.KeyCode;
//import javafx.scene.input.KeyCodeCombination;
//import javafx.scene.input.KeyCombination;
import com.tulskiy.keymaster.input.KeyCode;
import com.tulskiy.keymaster.input.KeyCodeCombination;
import com.tulskiy.keymaster.input.KeyCombination;

import static com.tulskiy.keymaster.input.KeyCode.*;

/**
 * Author: Denis Tulskiy
 * Date: 6/17/11
 */
public class KeyMap {
    private static Map<Integer, Integer> codes = new HashMap<Integer, Integer>() {{
        put(BACK_QUOTE.getCode(), 50);
        put(DIGIT1.getCode(), 18);
        put(DIGIT2.getCode(), 19);
        put(DIGIT3.getCode(), 20);
        put(DIGIT4.getCode(), 21);
        put(DIGIT5.getCode(), 23);
        put(DIGIT6.getCode(), 22);
        put(DIGIT7.getCode(), 26);
        put(DIGIT8.getCode(), 28);
        put(DIGIT9.getCode(), 25);
        put(DIGIT0.getCode(), 29);
        put(MINUS.getCode(), 27);
        put(UNDERSCORE.getCode(), 27);
        put(PLUS.getCode(), 24);
        put(EQUALS.getCode(), 24);
        put(BACK_SPACE.getCode(), 51);
        put(TAB.getCode(), 48);
        put(Q.getCode(), 12);
        put(W.getCode(), 13);
        put(E.getCode(), 14);
        put(R.getCode(), 15);
        put(T.getCode(), 17);
        put(Y.getCode(), 16);
        put(U.getCode(), 32);
        put(I.getCode(), 34);
        put(O.getCode(), 31);
        put(P.getCode(), 35);
        put(BRACELEFT.getCode(), 33);
        put(BRACERIGHT.getCode(), 30);
        put(BACK_SLASH.getCode(), 42);
        put(A.getCode(), 0);
        put(S.getCode(), 1);
        put(D.getCode(), 2);
        put(F.getCode(), 3);
        put(G.getCode(), 5);
        put(H.getCode(), 4);
        put(J.getCode(), 38);
        put(K.getCode(), 40);
        put(L.getCode(), 37);
        put(COLON.getCode(), 41);
        put(SEMICOLON.getCode(), 41);
        put(QUOTE.getCode(), 39);
        put(ENTER.getCode(), 36);
        put(Z.getCode(), 6);
        put(X.getCode(), 7);
        put(C.getCode(), 8);
        put(V.getCode(), 9);
        put(B.getCode(), 11);
        put(N.getCode(), 45);
        put(M.getCode(), 46);
        put(COMMA.getCode(), 43);
        put(PERIOD.getCode(), 47);
        put(SLASH.getCode(), 44);
        put(SPACE.getCode(), 49);
        put(F1.getCode(), 122);
        put(F2.getCode(), 120);
        put(F3.getCode(), 99);
        put(F4.getCode(), 118);
        put(F5.getCode(), 96);
        put(F6.getCode(), 97);
        put(F7.getCode(), 98);
        put(F8.getCode(), 100);
        put(F9.getCode(), 101);
        put(F10.getCode(), 109);
        put(F11.getCode(), 103);
        put(F12.getCode(), 111);
        put(ESCAPE.getCode(), 53);
        put(INSERT.getCode(), 114);
        put(HELP.getCode(), 114);
        put(HOME.getCode(), 115);
        put(PAGE_UP.getCode(), 116);
        put(DELETE.getCode(), 117);
        put(END.getCode(), 119);
        put(PAGE_DOWN.getCode(), 121);
        put(UP.getCode(), 126);
        put(DOWN.getCode(), 125);
        put(LEFT.getCode(), 123);
        put(RIGHT.getCode(), 124);
        put(EQUALS.getCode(), 81);
        put(DIVIDE.getCode(), 75);
        put(MULTIPLY.getCode(), 67);
        put(SUBTRACT.getCode(), 78);
        put(ADD.getCode(), 69);
        put(SUBTRACT.getCode(), 78);
        put(NUMPAD0.getCode(), 82);
        put(NUMPAD1.getCode(), 83);
        put(NUMPAD2.getCode(), 84);
        put(NUMPAD3.getCode(), 85);
        put(NUMPAD4.getCode(), 86);
        put(NUMPAD5.getCode(), 87);
        put(NUMPAD6.getCode(), 88);
        put(NUMPAD7.getCode(), 89);
        put(NUMPAD8.getCode(), 91);
        put(NUMPAD9.getCode(), 92);
        put(DECIMAL.getCode(), 65);
    }};

    public static int getKeyCode(KeyCode keyStroke) {
        //Integer ret = codes.get(keyStroke.getKeyCode());
        Integer ret = codes.get(keyStroke.getCode());
        if (ret != null)
            return ret;
        else
            return -1;
    }

    public static int getModifier(KeyCodeCombination keyStroke) {
        /**
         * liubinghua modified on 2023-11-07 23:05:05
         */
        int modifiers = 0;
        /*if ((keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0) {
            modifiers |= shiftKey;
        }
        if ((keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0) {
            modifiers |= controlKey;
        }
        if ((keyStroke.getModifiers() & InputEvent.META_DOWN_MASK) != 0) {
            modifiers |= cmdKey;
        }
        if ((keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0) {
            modifiers |= optionKey;
        }*/

        if (keyStroke.getShift() == KeyCombination.ModifierValue.DOWN) {
            modifiers |= shiftKey;
        }
        if (keyStroke.getControl() == KeyCombination.ModifierValue.DOWN) {
            modifiers |= controlKey;
        }
        if (keyStroke.getMeta() == KeyCombination.ModifierValue.DOWN) {
            modifiers |= cmdKey;
        }
        if (keyStroke.getAlt() == KeyCombination.ModifierValue.DOWN) {
            modifiers |= optionKey;
        }
        return modifiers;
    }
}
